﻿<style>
    .pt20 {
        padding-top: 0px;
    }

    .jg-right {
        padding-top: 20px;
    }
</style>
<template>
    <div>
        <div class="jg-left pull-left ft14" style="    width: 200px;">
            <p class="ft16 padding20 c999"><i class="iconfont icon-zuzhi ft14 mr5"></i>组织机构</p>
            <ul class="jg-list-box " style="padding-left:10px">
                <el-tree class="filter-tree"
                         :data="BranchListData"
                         :props="defaultProps"
                         :expand-on-click-node="iszk"
                         :default-expanded-keys="[1]"
                         node-key="DeptCode"
                         @node-click="BranchClick"
                         ref="tree">
                </el-tree>
            </ul>
        </div>
        <div class="jg-right " style="border: 0PX;    margin-left: 200px;">
            <div class="btn-add">
                <!--<button type="button" class="btn btn-info" @click="TBGZSTATUS()" v-if="CompanyData.IsUseWX=='Y'">同步关注状态</button>-->
                <button type="button" class="btn btn-info" @click="UserAdd()"><i class="iconfont icon-jiahao ft12 mr5"></i>添加员工</button>
                <button type="button" class="btn btn-info" @click="Userglz()"><i class="iconfont icon-dr ft12 mr5"></i>批量导入</button>

            </div>
            <!-- 机构管理详情 -->
            <div class="jg-cont ft14">

                <div class=" ml15" style="position: relative">

                    <a class="c-default dropdown-toggle ft16" data-toggle="dropdown">
                        <i class="iconfont icon-bookmark c-default mr5"></i>{{SelDeptItem.DeptName}}<span class="caret ml10"></span>
                    </a>
                    <ul class="dropdown-menu  animated fadeInRight">
                        <li>
                            <a @click="OpenBranch(1,SelDeptItem)">添加子部门</a>
                        </li>
                        <li>
                            <a @click="OpenBranch(2,SelDeptItem)">修改部门</a>
                        </li>
                        <li v-if="SelDeptItem.DeptRoot!=-1">
                            <a @click="DelDept(SelDeptItem,$event)">删除</a>
                        </li>
                        <li>
                            <a @click="OpenSetLeader()">设置部门负责人</a>
                        </li>


                    </ul>
                </div>
                <div class="detail-con">
                    <div class="detail-item">
                        <span class="item-tit">部门负责人：</span>
                        <div class="item-con">{{SelDeptItem.BranchLeader?SelDeptItem.BranchLeader:""}}<a @click="OpenSetLeader()" class="text-primary" style="margin-left: 20px;">设置</a></div>
                    </div>

                </div>
            </div>
            <!--  -->
            <div class="ft14">
                <div class="ml15 pb20 mr15">
                    <button type="button" class="btn btn-info" @click="UPDATEPLUser()">分配部门</button>
                    <button type="button" class="btn btn-info" @click="OpenPLModify()">重置密码</button>
                    <!--<button type="button" class="btn btn-info" v-if="CompanyData.FileServerUrl" @click="TBLotusUser()">同步开通Lotus用户</button>-->


                    <div class="input-group ml15" style="width: 295px; float: right; margin-right: 20px;display:none">
                        <input type="search" class="form-control" placeholder="搜索姓名，账号，部门，手机号" v-model="searchContent">
                        <span class="input-group-addon" @click="GetLIST()">搜索</span>
                    </div>
                </div>
                <div class=" ft14 pt20" style="padding-left: 15px;padding-right: 15px;">
                    <base-table :pzoption="tableop"  @selection-change="handleSelectionChange">
                    </base-table>
                </div>


            </div>
        </div>
        <div class="modal" id="BranchLeadModal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document" style="width: 500px">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">设置部门负责人</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-xs-4 control-label">负责人</label>
                                <input type="text" class="form-control szhl_getPeoples" v-model="branchLeader" style="display: none" signle="Y"  id="bmfzr"/>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-success" id="conaddForder" @click="SetLeader()"><i class="fa fa-spinner fa-spin" style="display: none"></i>保存</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal" id="UserLeadModal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document" style="width: 500px">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">设置上级</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-xs-4 control-label">上级人员</label>
                                <input type="text" class="form-control szhl_getPeoples" v-model="userLead" style="display: none" signle="Y" id="userleader" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-success" id="conaddForder" @click="SetUserLeader()"><i class="fa fa-spinner fa-spin" style="display: none"></i>保存</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="ReSetPassWordModal" role="dialog">
            <div class="modal-dialog" role="document" style="width: 500px">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">重置密码</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-xs-4 control-label"><span class="Required">*</span>新密码</label>
                                <input type="password" class="form-control szhl_require" style="width: 200px" id="newPwd" placeholder="请输入新密码" />
                            </div>
                            <div class="form-group">
                                <label class="col-xs-4 control-label"><span class="Required">*</span>确认密码</label>
                                <input type="password" class="form-control szhl_require" style="width: 200px" id="newPwd2" placeholder="请输入确认密码" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-success" id="conaddForder" @click="ModifyPwd(this)"><i class="fa fa-spinner fa-spin" style="display: none"></i>确&nbsp;&nbsp;认</button>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="ModifyBranchModal" role="dialog">
            <div class="modal-dialog" role="document" style="width: 500px">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">编辑部门</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-xs-4 control-label text-right">上级部门</label>
                                <div class="col-xs-6">
                                    <input type="text" class="form-control" id="pname" readonly="readonly" />
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="branchName" class="col-xs-4 control-label text-right">部门名称</label>
                                <div class="col-xs-6">
                                    <input type="text" class="form-control" id="branchName" v-model="BranchData.DeptName" placeholder="请输入部门名称（必填）" />
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="branchDesc" class="col-xs-4 control-label text-right">部门描述</label>
                                <div class="col-xs-6">
                                    <input type="text" class="form-control" v-model="BranchData.DeptDesc" id="branchDesc" placeholder="请输入部门描述" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="branchShort" class="col-xs-4 control-label text-right">房间号</label>
                                <div class="col-xs-6">
                                    <input type="text" class="form-control" v-model="BranchData.RoomCode" placeholder="请输入房间号" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="branchShort" class="col-xs-4 control-label text-right">排序</label>
                                <div class="col-xs-6">
                                    <input type="text" class="form-control" v-model="BranchData.DeptShort" id="branchShort" placeholder="请输入部门排序" />
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-success" id="conaddForder" @click="BranchAdd()"><i class="fa fa-spinner fa-spin" style="display: none"></i>确&nbsp;&nbsp;认</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    module.exports = {
        name: 'xtgl_ygindex',
        props: ['pdata'],
        components: {
            'base-table': httpVueLoader('/ViewV5/Base/Vue/Table.vue')
        },
        data: function () {
            return {
                iszk: false,
                BranchListData: [],
                UserData: [],
                searchContent: "",
                SelDeptItem: {},
                totalUserCount: 0,
                UserPrintData: [],
                branchLeader: "",
                userLead: "",
                selUserName: "",
                resetUser: "",
                dexdata:[],
                BranchData: { "DeptName": "", DeptDesc: "", DeptShort: "0", DeptRoot: "", RoomCode: "" },
                tableop: {
                    title: "人员表格",
                    issel: true,
                    iscz: true,
                    dataset: [],
                    multipleSelection:[],
                    czltabledata: [
                        {
                            colname: "设置上级", bttype: "text", isshow: true, mangefun: function (item) {
                                var pro = this.$parent;
                                $('#UserLeadModal').modal('show');
                                pro.selUserName = item;
                                pro.userLead = item.UserLeader;
                                ComFunJS.initForm();
                            },
                        },
                        {
                            colname: "编辑", bttype: "text", isshow: true, mangefun: function (item) {
                                ComFunJS.winviewform("/ViewV5/AppPage/APP_ADD.html?FormCode=XTGL_YGGL&ID=" + item.UserName + "&userName=" + item.UserName, '编辑员工', "1000", "");
                            },
                        },
                        {
                            colname: "删除", bttype: "text", isshow: true,  mangefun: function (rowdata) {
                                var pro = this.$parent;
                                if (confirm("确定要删除" + rowdata.UserName + "吗？")) {
                                    $.getJSON("/api/Auth/ExeAction?Action=DELUSER", { P1: rowdata.UserName },
                                        function (result) {
                                            if ($.trim(result.ErrorMsg) == "") {
                                                pro.GetLIST();
                                                top.ComFunJS.winsuccess("操作成功");

                                            }
                                        });
                                }
                            }
                        }
                    ],
                    collist: [
                        { colid: "UserRealName", colname: "姓名", isshow: true, istip: true },
                        { colid: "DeptName", colname: "部门", isshow: true, istip: true },
                        { colid: "UserName", colname: "用户名", isshow: true, istip: true },
                        { colid: "ROLENAME", colname: "所属角色", isshow: true, istip: true },
                        { colid: "mobphone", colname: "手机号码", isshow: true, istip: true }

                    ]
                },
                defaultProps: {
                    children: 'ChildBranch',
                    label: 'DeptName'
                }
            }
        },
        methods: {
            handleSelectionChange(val) {
                this.tableop.multipleSelection = val;
            },
            GetBranch: function () {
                var tempindex = this;
                $.getJSON('/api/Auth/ExeAction?Action=GETALLBMNEW', {}, function (resultData) {
                    tempindex.BranchListData = resultData.Result;
                    tempindex.SelDeptItem = resultData.Result[0];
                    tempindex.GetLIST();
                })
            },

            BranchClick: function (item) {
                this.SelDeptItem = {};
                this.SelDeptItem = item;
                this.GetLIST();
            },
            dcyg: function () {
                if (model.page.total > 0) {
                    $(this).attr("href", "/api/Auth/EXPORTYG?&P1=" + this.SelDeptItem.DeptCode + "&P2=" + this.searchContent + "&pagecount=" + model.page.total);
                }
                else {
                    ComFunJS.winwarning("无数据");
                }
            },
            GetLIST: function () {
                var tempindex = this;
                $.getJSON('/api/Auth/ExeAction?Action=GETUSERBYCODENEWPAGE', { "P1": this.SelDeptItem.DeptCode, P2: this.searchContent, pagecount: 5000, p: 1 }, function (resultData) {
                    if (resultData.ErrorMsg == "") {
                        tempindex.tableop.dataset = resultData.Result;

                    }
                })
            },

            OpenSetLeader: function () {
                var tempindex = this;
                $('#BranchLeadModal').modal('show');
                tempindex.branchLeader = tempindex.SelDeptItem.BranchLeader;
                ComFunJS.initForm();
            },
            SetLeader: function () {
                var tempindex = this;
                $.getJSON("/api/Auth/ExeAction?Action=SETBRANCHLEADER", { P1: $("#bmfzr").val(), P2: tempindex.SelDeptItem.DeptCode },
                    function (result) {
                        if (result.ErrorMsg == "") {
                            tempindex.SelDeptItem.BranchLeader = $("#bmfzr").val();
                            top.ComFunJS.winsuccess("设置负责人成功");
                            $('#BranchLeadModal').modal('hide');
                        }

                    });
            },

            OpenUserLead: function (item) {
                var tempindex = this;
                $('#UserLeadModal').modal('show');
                tempindex.selUserName = item;
                tempindex.userLead = item.UserLeader;
                ComFunJS.initForm();
            },
            SetUserLeader: function () {
                var tempindex = this;
                $.getJSON("/api/Auth/ExeAction?Action=SETUSERLEADER", { P1: $("#userleader").val(), P2: tempindex.selUserName.UserName },
                    function (result) {
                        if ($.trim(result.ErrorMsg) != "") {
                            top.ComFunJS.winwarning(result.ErrorMsg);
                        } else {
                            top.ComFunJS.winsuccess("设置上级成功");
                            tempindex.selUserName.UserLeader = tempindex.userLead;
                            $('#UserLeadModal').modal('hide');
                        }
                    })
            },
            EditUser: function (item) {
                ComFunJS.winviewform("/ViewV5/AppPage/APP_ADD.html?FormCode=XTGL_YGGL&ID=" + item.UserName + "&userName=" + item.UserName, '编辑员工', "1000", "");
            },
            DelDept: function (item, event) {
                if (event) {
                    event.stopPropagation();
                }
                if (confirm("确定要删除" + item.DeptName + "吗？")) {
                    $.getJSON("/api/Auth/ExeAction?Action=DELBRANCH", { P1: item.DeptCode },
                        function (result) {
                            if (result.ErrorMsg == "") {
                                top.ComFunJS.winsuccess("删除成功");
                            }
                        });
                }
            },
            DelUser: function (item) {
                var tempindex = this;
                if (confirm("确定要删除" + item.UserName + "吗？")) {
                    $.getJSON("/api/Auth/ExeAction?Action=DELUSER", { P1: item.UserName },
                        function (result) {
                            if (result.ErrorMsg == "") {
                                tempindex.UserData.remove(item);
                                top.ComFunJS.winsuccess("操作成功");

                            }
                        });
                }
            },
            EditIsUse: function (item) {
                var tempindex = this;
                $.getJSON("/api/Auth/ExeAction?Action=UPDATEUSERISUSE", { P1: item.UserName, P2: (item.IsUse == "Y" ? "N" : "Y") },
                    function (result) {
                        if ($.trim(result.ErrorMsg) == "") {
                            top.ComFunJS.winsuccess(item.IsUse == "Y" ? "禁用成功" : "启用成功");
                        }
                    });
            },
            UserAdd: function () {
                var tempindex = this;
                var deptCode = tempindex.SelDeptItem.DeptCode;
                if (!deptCode) {
                    deptCode = 1;
                }
                if ($.trim(deptCode) != "") {
                    top.ComFunJS.winviewform("/ViewV5/AppPage/APP_ADD.html?FormCode=XTGL_YGGL&branchNo=" + deptCode, "添加用户", "1000");
                }
            },
            UserImport: function () {
            
                top.ComFunJS.winviewform("/ViewV5/AppPage/XTGL/IMPORTUSER.html", "导入用户", 1000, 600);
            },
            Userglz: function () {
                var tempindex = this;
                var deptCode = tempindex.SelDeptItem.DeptCode;
               // top.ComFunJS.winviewform("/ViewV5/Base/ImportExcel.html?zid=" + deptCode, "导入用户", 1000, 600);
                top.ComFunJS.winviewform("/ViewV5/AppPage/XTGL/IMPORTUSER.html", "导入用户", 1000, 600);

            },

            UPDATEUser: function (item) {
                top.ComFunJS.winbtnwin("/ViewV5/AppPage/XTGL/BranchSelect.html?checkvalue=" + item.BranchCode, "修改部门", 500, 470, {}, function (layero, index) {
                    var frameid = $("iframe", $(layero)).attr('id');
                    var branch = ComFunJS.isIE() ? top.window.frames[frameid].getselectedbranch() : top.window.frames[frameid].contentWindow.getselectedbranch();
                    item.BranchCode = branch.branchid;
                    var modelValue = JSON.stringify(item);
                    $.getJSON("/api/Auth/ExeAction?Action=ADDUSER", { P1: modelValue },
                        function (result) {
                            if ($.trim(result.ErrorMsg) == "") {
                                item.DeptName = branch.branchname;
                                top.layer.close(index)
                                top.ComFunJS.winsuccess("设置成功");
                            }
                        });
                })
            },
            UPDATEPLUser: function () {
                var ids = [];
                for (var i = 0; i < this.tableop.multipleSelection.length; i++) {
                    ids.push(this.tableop.multipleSelection[i].UserName);
                }
                if (ids.length == 0) {
                    this.$notify.error({
                        title: '错误',
                        message: '至少需要选择一条数据才能操作'
                    });
                    return;
                }
                top.ComFunJS.winbtnwin("/ViewV5/AppPage/XTGL/BranchSelect.html", "批量修改部门", 500, 470, {}, function (layero, index) {
                    var frameid = $("iframe", $(layero)).attr('id');
                    var branch = ComFunJS.isIE() ? top.window.frames[frameid].getselectedbranch() : top.window.frames[frameid].contentWindow.getselectedbranch();
                    var strUser = ids.join();
                    if (strUser) {
                        $.getJSON("/api/Auth/ExeAction?Action=PLSETBRANCH", { P1: strUser, P2: branch.branchid },
                            function (result) {
                                if ($.trim(result.ErrorMsg) == "") {
                                   // tempindex.BranchClick(tempindex.SelDeptItem);
                                    top.layer.close(index)
                                    top.ComFunJS.winsuccess("设置成功");
                                }
                            });
                    }
                })
            },
            OpenModify: function (user) {
                this.resetUser = user.UserName;
                $('#ReSetPassWordModal').modal('show');
            },
            OpenPLModify: function () {
                var ids = [];
                for (var i = 0; i < this.tableop.multipleSelection.length; i++) {
                    ids.push(this.tableop.multipleSelection[i].UserName);
                }
                if (ids.length == 0) {
                    this.$notify.error({
                        title: '错误',
                        message: '至少需要选择一条数据才能操作'
                    });
                    return;
                }
                if (ids.join()) {
                    this.resetUser = ids.join();
                    $('#ReSetPassWordModal').modal('show');
                }
            },
            TBLotusUser: function () {
                var userNames = $("#branchTab tbody tr td .checked");
                if (userNames.length == 0) {

                    top.ComFunJS.winwarning("请选择要同步的用户");
                    return;
                }
                var strUser = ""
                $(userNames).each(function (i, item) {
                    strUser += $(item).attr("user") + "$" + $(item).attr("realuser") + ",";
                })
                strUser = strUser.substring(0, strUser.length - 1);
                if (strUser) {
                    if (confirm("确定要同步用户吗？")) {
                        $.getJSON("/api/Bll/ExeAction?Action=QYWD_TBLOTUSUSER", { P1: strUser },
                            function (result) {
                                if ($.trim(result.ErrorMsg) == "") {
                                    top.ComFunJS.winsuccess("操作成功");
                                }
                            });
                    }
                }
            },
            ModifyPwd: function () {
                var pwd = $("#newPwd").val();
                var pwd2 = $("#newPwd2").val();
                var retmsg = "";
                if ($("#ReSetPassWordModal .szhl_require")) {
                    $("#ReSetPassWordModal .szhl_require").each(function () {
                        if ($(this).val() == "") {
                            retmsg = $(this).parent().find("label").text() + "不能为空";
                        }
                    })
                }
                if (retmsg !== "") {
                    top.ComFunJS.winwarning(retmsg);
                    return;
                }
                if (pwd != pwd2) {
                    retmsg = "确认密码不一致";
                    top.ComFunJS.winwarning(retmsg);
                    return;
                }
                $.getJSON("/api/Auth/ExeAction?Action=MODIFYPWD", { P1: pwd, P2: pwd2, username: this.resetUser }, function (result) {
                    if ($.trim(result.ErrorMsg) == "") {

                        top.ComFunJS.winsuccess("操作成功");
                        $('#ReSetPassWordModal').modal('hide');
                    }
                });
                $("#newPwd").val("");
                $("#newPwd1").val("");
            },
            OpenBranch: function (type, branch, pbranch) {
                var tempindex = this;
                if (type == 1)//添加部门
                {
                    this.BranchData = { DeptName: "", DeptDesc: "", DeptShort: "0", DeptRoot: branch.DeptCode, RoomCode: "" };
                    $("#pname").val(branch.DeptName);
                } else {
                    if (branch.DeptCode) {
                        //获取主要菜单
                        $.getJSON('/api/Auth/ExeAction?Action=GETBRANCHBYCODE', { "P1": branch.DeptCode }, function (resultData) {
                            if (resultData.ErrorMsg == "") {
                                tempindex.BranchData = resultData.Result;
                                $("#pname").val(resultData.Result1.DeptName);
                            }
                        })
                    }
                }
                $('#ModifyBranchModal').modal('show');
            },
            BranchAdd: function () {
                debugger;
                if (!this.BranchData.DeptName) {
                    ComFunJS.winwarning("请输入部门名称");
                    return;
                }
                var reg = /^\d+$/;
                if (!reg.test(this.BranchData.DeptShort)) {
                    ComFunJS.winwarning("排序必须为数字");
                    return;
                }
                var modelValue = JSON.stringify(this.BranchData);
                $.getJSON("/api/Auth/ExeAction?Action=ADDBRANCH", { P1: modelValue },
                    function (result) {
                        if ($.trim(result.ErrorMsg) == "") {
                            ComFunJS.winsuccess("操作成功");
                            $('#ModifyBranchModal').modal('hide');
                            model.refpage('YGINDEX');
                        }
                    });
            },
            gzStatus: function (type) {
                var typeName = "未关注";
                if (type == 1) {
                    typeName = "已关注";
                }
                return typeName;
            },
            TBGZSTATUS: function () {
                $.getJSON('/api/Auth/ExeAction?Action=TBGZSTATUS', {}, function (resultData) {
                    if (resultData.ErrorMsg == "") {
                        top.ComFunJS.winsuccess("同步成功");
                        this.GetLIST();
                    }
                })

            }
        },
        mounted: function () {
            var pro = this;
            pro.$nextTick(function () {
                pro.GetBranch();
                ComFunJS.initForm();
            })

        },
        watch: {
            childpro: { //深度监听，可监听到对象、数组的变化
                handler(newV, oldV) {
                },
                deep: true
            }
        }
    };
</script>